#!/bin/bash
ERRNODEV=10
ERRNOMETHOD=11
ERRNOUSER=12
ERRARGU=13
ERRDONE=0
DEVLIST=/Users/`whoami`/.devices.lst

show_device_list()
{
    printf "  %-20s   %-10s   %-20s\n" DEVICE CONN ADDR
    echo  "============================================================"
    awk '{if ($0!~/^#/ && $0!~/^$/)
        {printf("| %-20s|  %-10s|  %-20s |\n", $1,$2,$3,$4)
        printf("------------------------------------------------------------\n")}
        }' $DEVLIST
}
dev_name_list=$(awk '{if ($0!~/^#/ && $0!~/^$/) {print $1}}' ~/.devices.lst |tr '\n' ' ')
autocomp="_conn_completion()\n
{\n
local cur;
COMPREPLY=();cur=\${COMP_WORDS[COMP_CWORD]};COMPREPLY=(\$( compgen -W '$dev_name_list' -- \$cur ));\n
return 0\n
}\n
complete -F _conn_completion conn\n
"



help()
{
    echo "$0 <device_name> [connection_method] [<user_name>]"
    echo "Here is device in your device list:"
    show_device_list
    echo -e $autocomp > ~/.conn.ac
    echo "If it's blank, you need to create device list manually"
    echo "[Notice]:"
    echo "If you want to use auto complete function, run following command manually"
    echo "# source ~/.conn.ac"
    echo ""
}

connect_ssh()
{
    record=$1
    host=$(echo $record|awk '{print $3}'|cut -d: -f1)
    port=$(echo $record|awk '{print $3}'|cut -d: -f2)
    
    user=$(echo $record|awk '{print $4}'|cut -d: -f1)
    passwd=$(echo $record|awk '{print $4}'|cut -d: -f2)
    
    expect -c "
    set timeout 3
    spawn ssh ${user}@${host} -p ${port}
    expect {
        \"(yes/no)\" { send \"yes\r\"; exp_continue; }
        \"*assword\" { send \"${passwd}\r\"; exp_continue; }
        \"*please try again*\" { puts \"Password wrong, please check\" return }
    }
    expect {
        \"#\" { interact }
        \"$\" { interact }
    }
    "
}

start_proxy()
{
    record=$1
    host=$(echo $record|awk '{print $6}'|cut -d: -f1)
    port=$(echo $record|awk '{print $6}'|cut -d: -f2)
    
    user=$(echo $record|awk '{print $7}'|cut -d: -f1)
    passwd=$(echo $record|awk '{print $7}'|cut -d: -f2)
    local_port=$(echo $record|awk '{print $3}')
    dst_ip=$(echo $record|awk '{print $4}')
    dst_port=$(echo $record|awk '{print $5}')
    #user="-L ${local_port}:${dst_ip}:${dst_port} ${user}"
    #expect -c $ssh_exp
    expect -c "
    set timeout 3
    spawn ssh -L ${local_port}:${dst_ip}:${dst_port} ${user}@${host} -p ${port}
    expect {
        \"(yes/no)\" { send \"yes\r\"; exp_continue; }
        \"*assword\" { send \"${passwd}\r\"; exp_continue; }
        \"*please try again*\" { puts \"Password wrong, please check\" return }
    }
    expect {
        \"#\" { interact }
        \"$\" { interact }
    }
    "
}

connect_console()
{
    record=$1
    host=$(echo $record|awk '{print $3}'|cut -d: -f1)
    port=$(echo $record|awk '{print $3}'|cut -d: -f2)
    
    user=$(echo $record|awk '{print $4}'|cut -d: -f1)
    passwd=$(echo $record|awk '{print $4}'|cut -d: -f2)
    expect -c "
    set timeout 10
    spawn telnet ${host} ${port}
    send "\r"
    expect {
    }
    "

}

if [ $# -lt '1' ];then
    help
    exit $ERRARGU
fi
BOX=$1
#CON=$2
#USER=$3

ret=`grep $BOX $DEVLIST`

if [ -z "$ret" ]
then
    echo "No device and connection method matched, please check your device list"
    echo "Devices in your device list:"
    show_device_list
    exit $ERRNODEV
else
    echo "Device found in list, connecting.."
    conn_method=$(echo $ret|awk '{print $2}')
    if [ $conn_method == 'console' ];then
        echo -e "Predefined connection method is [\033[1;31mconsole\033[0m]."
        telnet `echo $ret| awk '{print $3}'|awk -F\: '{print $1,$2}'`
        exit $ERRDONE
    elif [ $conn_method == 'ssh' ];then
        echo -e "Predefined connection method is [\033[1;31mSSH\033[0m]."
        connect_ssh "$ret"
        echo "Connection closed"
        exit $ERRDONE
    elif [[ $conn_method == 'proxy' ]]; then
        echo -e "Predefined connection method is [\033[1;31mProxy\033[0m]."
        start_proxy "$ret"
        echo "Proxy stopped."
        exit $ERRDONE
    else
        echo "Predefined connection method is not supported yet"
        exit $ERRNOMETHOD
    fi
fi

